Completed
Push — master ( e01337...7c5d19 )
by
unknown
02:22
created

get-main.js ➔ ... ➔ ???   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
nc 5
dl 0
loc 31
rs 6.7272
c 4
b 0
f 0
cc 7
nop 1
1
import xss from 'xss'
2
import path from 'path'
3
import pkg from '../../../package'
4
5
import {
6
  config,
7
  Page,
8
  cmsData,
9
  cmsTemplates,
10
  coreUtils,
11
  abeExtend,
12
  Manager
13
} from '../../cli'
14
15
import {editor} from '../controllers/editor'
16
import locale from '../helpers/abe-locale'
17
18
var route = function(req, res, next) {
19
  if(req.query.filePath){
20
    var testXSS = xss(req.query.filePath, {
21
      whiteList: [],
22
      stripIgnoreTag: true
23
    })
24
    if(testXSS !== req.query.filePath){
25
      res.redirect(`/abe/${req.params[0]}?filePath=${testXSS}`)
26
      return
27
    }
28
  }
29
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
30
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
31
32
  var templatePath = req.params[0]
33
34
  var filePath = null
35
  if(typeof req.query.filePath !== 'undefined' && req.query.filePath !== null) {
36
    filePath = path.join(config.root, config.draft.url, req.query.filePath.replace(config.root))
37
  }
38
  var debugJson = (req.query.debugJson && req.query.debugJson == 'true' ) ? true : false
39
  var debugJsonKey = (req.query.key) ? req.query.key : false
40
  var debugHtml = (req.query.debugHtml && req.query.debugHtml == 'true' ) ? true : false
41
42
  var isHome = true
43
44
  let p = new Promise((resolve) => {
45
46
    if(templatePath !== null && filePath !== null) {
47
      var jsonPath = null
48
      var linkPath = null
49
      isHome = false
50
51
      var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath)
52
      if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
53
        jsonPath = filePathTest.path
54
        linkPath = filePathTest.abe_meta.link
55
      }
56
57
      if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { 
58
        res.redirect('/abe/') 
59
        return 
60
      }
61
62
      editor(templatePath, jsonPath, linkPath)
63
        .then((result) => {
64
          resolve(result)
65
        }).catch(function(e) {
66
          console.error(e)
67
        })
68
    }else {
69
      resolve({
70
        obj: {},
71
        manager: {}
72
      })
73
    }
74
  }).catch(function(e) {
75
    console.error(e) // "oh, no!"
76
  })
77
78
  p.then((result) => {
79
    var obj = result
80
    var manager = {}
81
  
82
    manager.home = {
83
      files: Manager.instance.getList()
84
    }
85
86
    manager.list = Manager.instance.getStructureAndTemplates()
87
    manager.editConfig = req.app.get('config')
88
    manager.config = JSON.stringify(config)
89
    
90
    var _hasBlock = (obj) ? obj.hasBlock : false
91
    var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false
92
    var _template = (filePath) ? '/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false
93
    var _form = (obj) ? obj.form : false
94
    var _json = (obj) ? obj.json : false
95
    var _text = (obj) ? obj.text : false
96
    // var _file = (tplUrl) ? tplUrl.draft.file : false
97
    var _filePath = (req.query.filePath) ? req.query.filePath : false
98
    if (_filePath) {
99
      _filePath = '/' + _filePath.replace(/^\/+/, '')
100
    }
101
102
    var pageHtml = '' 
103
    if(typeof _json !== 'undefined' && _json !== null 
104
      && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) { 
105
      var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) 
106
      var page = new Page(_json.abe_meta.template, text, _json, false) 
107
      pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>')
108
    } 
109
110
    var EditorVariables = {
111
      pageHtml: pageHtml,
112
      isHome: isHome,
113
      abeUrl: '/abe/',
114
      test: JSON.stringify(locale),
115
      text: locale,
116
      templatePath: req.params[0],
117
      template: _template,
118
      hasSingleBlock: _hasSingleBlock,
119
      hasBlock: _hasBlock,
120
      form: _form,
121
      urlToSaveFile: _filePath,
122
      folderToSaveFile: (_filePath) ? path.dirname(_filePath) : '',
123
      // tplName: _file,
124
      json: _json,
125
      config: config,
126
      Locales: coreUtils.locales.instance.i18n,
127
      manager: manager,
128
      express: {
129
        res: res,
130
        req: req
131
      },
132
      abeVersion: pkg.version,
133
      nonce: '\'nonce-' + res.locals.nonce + '\''
134
    }
135
    EditorVariables = abeExtend.hooks.instance.trigger('afterVariables', EditorVariables)
136
137
    if (debugJson) {
138
      var dj = _json
139
      if(debugJsonKey && typeof dj[debugJsonKey] !== 'undefined' && dj[debugJsonKey] !== null) {
140
        dj = dj[debugJsonKey]
141
      }
142
      res.set('Content-Type', 'application/json')
143
      res.send(JSON.stringify(dj))
144
    }else if (debugHtml) {
145
      res.set('Content-Type', 'text/plain')
146
      res.send(_text)
147
    }else {
148
      res.render(config.abeEngine, EditorVariables)
149
    }
150
  }).catch((e) => {
151
    console.log('error', e)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
152
  })
153
}
154
155
export default route